﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace SocketService
{

    #region 委托
    public delegate void CommendEventHandler(string msg, Socket socket);
    public delegate void CommendEventHandler2(string msg); 
    #endregion
    /// <summary>
    /// 套接字监听
    /// by:qnl
    /// </summary>
    public class SocketListener
    {
        /// <summary>
        /// 收到信息后触发事件
        /// </summary>
        public CommendEventHandler Commendeven { get; set; }
        /// <summary>
        /// 收到信息后触发事件
        /// </summary>
        public CommendEventHandler2 CommendevenTwo { get; set; }

        TcpListener tcpListener;
        private bool state = true;
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="ip"></param>
        /// <param name="port"></param>
        public SocketListener(string ip, int port)
        {
            IPAddress tcpip = IPAddress.Parse(ip);
            try
            {
                tcpListener = new TcpListener(tcpip, port);
                tcpListener.Start();
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }
            ThreadPool.QueueUserWorkItem(ListenerStart);
        }

        /// <summary>
        /// 为每个请求在线程池中开辟一条通道
        /// </summary>
        /// <param name="client"></param>
        public void ListenerStart(object client)
        {
            while (state)
            {
                try
                {
                    
                    var socket = tcpListener.AcceptSocket();
                    ThreadPool.QueueUserWorkItem(ServiceListen, socket);
                }
                catch (Exception e)
                {
                    Console.WriteLine(e.Message);
                }
            }
        }

        /// <summary>
        /// 套接字监听
        /// </summary>
        public void ServiceListen(object objlisten)
        {
            Socket objSocket = (Socket)objlisten;
            int bytei = 0;
            try
            {
                byte[] buffer = new byte[2048];
                bytei = objSocket.Receive(buffer);
                string req = Encoding.Default.GetString(buffer);
                req = req.Substring(0, req.IndexOf("\0"));
                if (Commendeven!=null)
                {
                    Commendeven(req, objSocket);
                }
                if (CommendevenTwo!=null)
                {
                    CommendevenTwo(req);
                    if (objSocket!=null)
                    {
                        objSocket.Close();
                    }
                }
                
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }
        }

        /// <summary>
        /// 关闭监听
        /// </summary>
        public void Close()
        {
            state = false;
            tcpListener.Stop();
        }
        
    }
}
